package com.feng.Point02;

import java.io.BufferedReader;

/**
 * Created by FengBin on 2022/3/22 20:27
 *
 * 如果一个类的构造器或者工厂模式中存在多个参数，此时相对于重载构造器或者JavaBeans模式(parameters-set)方法
 * Builder模式(建造者模式的一种表现形式)是一种不错选择！
 * 以营养成分类：NutritionFacts为例，它存在以下属性:
 *  (1) servingSize 摄入量
 *  (2) servings 分量
 *  (3) calories 卡路里
 *  (4) sodium 纳
 *  (5) fat
 *  (6) carbohydrate 糖分
 */
public class NutritionFacts {
    private final int servingSize;
    private final int servings;
    private final int calories;
    private final int fat;
    private final int sodium;
    private final int carbohydrate;

    //定义一个构造者
    public static class Builder {
        private final int servingSize;
        private final int servings;

        private int calories = 0;
        private int fat = 0;
        private int sodium = 0;
        private int carbohydrate = 0;

        //Builder构造器
        public Builder(int servingSize, int servings) {
            this.servingSize = servingSize;
            this.servings = servings;
        }

        public Builder calories(int val) {
            calories = val;
            return this;
        }

        public Builder fat(int val) {
            fat = val;
            return this;
        }

        public Builder sodium(int val) {
            sodium = val;
            return this;
        }

        public Builder carbohydrate(int val) {
            carbohydrate = val;
            return this;
        }

        public NutritionFacts build() {
            return new NutritionFacts(this);
        }
    }

    //NutritionFacts的私有构造器
    private NutritionFacts(Builder builder) {
        servingSize = builder.servingSize;
        servings = builder.servings;
        calories = builder.calories;
        fat = builder.fat;
        sodium = builder.sodium;
        carbohydrate = builder.carbohydrate;
    }

    @Override
    public String toString() {
        return "NutritionFacts{" +
                "servingSize=" + servingSize +
                ", servings=" + servings +
                ", calories=" + calories +
                ", fat=" + fat +
                ", sodium=" + sodium +
                ", carbohydrate=" + carbohydrate +
                '}';
    }
}
